#include "stm32f10x.h"
#include "wdtx_timer.h"
#include "wdtx_lcd5110.h"
#include "calendar.h"
#include "exti.h"
#include "wdtx_at24cxx.h"
#include "wdtx_usart.h"

void Lcd_Configure(void);
void Eeprom_save(void);
void Eeprom_read(Calendar cal);
void Lcd_display(void);
void setCalendar(uint8_t* data);

uint16_t FLAG;
extern uint8_t keyState;

int main(void)
{	
	struct calendarStruct cal;

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	TIM3_Int_Init(9,7199);  //0.001s
	Lcd_Configure();
	LCD5510_Init();
	At24cxx_init();
	Usart_defaultInit();
	EXTIX_Init();
	
	Eeprom_read(&cal);
	Calendar_init(&cal);
	Lcd_display();
	
	while(1)
	{
		if(FLAG & 1)
		{
			//@ 1s
			Calendar_walk();
			FLAG &=~ 1;
		}
		if(FLAG & 1<<1)
		{
			Lcd_display();
			FLAG &=~ (1<<1);
		}
		if(FLAG & 1<<2)
		{
			Eeprom_save();
			FLAG &=~ (1<<2);
		}
		if((FLAG & 1<<3) && keyState)
		{
			Usart_SendCalendar(Calendar_getCalendar());
			keyState = 0;
		}
		if((FLAG & 1<<4) && Usart_HasReceivedData())
		{
			setCalendar(Usart_GetReceivedData());
			Usart_ClearReceivedBuffer();
		}
	}
}

void setCalendar(uint8_t* data)
{
	struct calendarStruct cal;
	cal.year = data[0]*1000 + data[1]*100 + data[2]*10 + data[3];
	cal.month = data[4] *10 + data[5];
	cal.day = data[6] *10 + data[7];
	cal.hour = data[8] *10 + data[9];
	cal.minute = data[10] *10 + data[11];
	cal.second = data[12] *10 + data[13];
	if(Calendar_judge(&cal))
	{
		Calendar_init(&cal);
	}
}

void Lcd_Configure()
{
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
}

void Eeprom_save()
{
	static uint8_t state = 0;
	Calendar cal = Calendar_getCalendar();
	state++;
	state %= 10;
	if(state == 0)
	{
		At24cxx_writeByte(1,cal->second);
	}
	else if(state == 1)
	{
		At24cxx_writeByte(2,cal->minute);
	}
	else if(state == 2)
	{
		At24cxx_writeByte(3,cal->hour);
	}
	else if(state == 3)
	{
		At24cxx_writeByte(4,cal->day);
	}
	else if(state == 4)
	{
		At24cxx_writeByte(5,cal->month);
	}
	else if(state == 5)
	{
		At24cxx_writeByte(6,cal->year >> 8);
	}
	else if(state == 6)
	{
		At24cxx_writeByte(7,cal->year);
	}
}

void Eeprom_read(Calendar cal)
{
	cal->second = At24cxx_readByte(1);
	cal->minute = At24cxx_readByte(2);
	cal->hour = At24cxx_readByte(3);
	cal->day = At24cxx_readByte(4);
	cal->month = At24cxx_readByte(5);
	cal->year = At24cxx_readByte(6);
	cal->year <<= 8;
	cal->year += At24cxx_readByte(7);
	
	if(!Calendar_judge(cal))
	{
		cal->second = 0;
		cal->minute = 0;
		cal->hour = 8;
		cal->day = 8;
		cal->month = 11;
		cal->year = 2016;
	}
}

void Lcd_display()
{
	uint16_t year;
	static uint8_t state = 0;
	Calendar cal = Calendar_getCalendar();
	if(cal->flag & 1)
	{
		LCD5110_printc(7,1,cal->second/10 + '0');
		LCD5110_printc(8,1,cal->second%10 + '0');
	}
	if(cal->flag & 1<<1)
	{
		LCD5110_printc(4,1,cal->minute/10 + '0');
		LCD5110_printc(5,1,cal->minute%10 + '0');
	}
	if(cal->flag & 1<<2)
	{
		LCD5110_printc(1,1,cal->hour/10 + '0');
		LCD5110_printc(2,1,cal->hour%10 + '0');
	}
	if(cal->flag & 1<<3)
	{
		LCD5110_printc(8,3,cal->day/10 + '0');
		LCD5110_printc(9,3,cal->day%10 + '0');
	}
	if(cal->flag & 1<<4)
	{
		LCD5110_printc(5,3,cal->month/10 + '0');
		LCD5110_printc(6,3,cal->month%10 + '0');
	}
	if(cal->flag & 1<<5)
	{
		year = cal->year;
		LCD5110_printc(3,3,year%10 + '0');
		year /= 10;
		LCD5110_printc(2,3,year%10 + '0');
		year /= 10;
		LCD5110_printc(1,3,year%10 + '0');
		year /= 10;
		LCD5110_printc(0,3,year%10 + '0');
	}
	if(cal->flag & 1<<6)
	{
		LCD5110_printc(4,3,'-');
		LCD5110_printc(7,3,'-');
	}
	if(state)
	{
		LCD5110_printc(3,1,':');
		LCD5110_printc(6,1,':');
		state = 0;
	}
	else
	{
		LCD5110_printc(3,1,' ');
		LCD5110_printc(6,1,' ');
		state = 1;
	}
	
	cal->flag = 0;
}


